package org.deng.learn.es.rest;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 邓力宾
 * @date 2020/12/25 11:56
 * @desc
 */
public class Main {
    private static  String INDEX_NAME = "com_user"; //索引名称 相当于数据库
    private static RestEsClient client = new RestEsClient();
    public static void main(String[] args) throws IOException {
       //testCreateIndex();
        //testDeleteIndex();
        //testExist();
        //testOpen();
        //testClose();
        testInsert();
        //testGet();
    }

    /**
     * 测试创建索引库
     * 查看mapping http://192.168.2.177:9200/com_user/_mapping?pretty
     * 查看setting http://192.168.2.177:9200/com_user/_settings?pretty
     */
    public static void testCreateIndex() throws IOException {
        // mapping中的properties属性,用于指定字段及字段的数据类型
        Map<String, FieldType> mappingProperties = new HashMap<>();
        //字段以及字段类型
        mappingProperties.put("author", new FieldType("keyword", true));
        mappingProperties.put("content", new FieldType("text", false));
        mappingProperties.put("fullContent", new FieldType("text", false));
        mappingProperties.put("createDate", new FieldType("date", true));
        mappingProperties.put("salary", new FieldType("integer", true));
        mappingProperties.put("likes", new FieldType("text", true));
        mappingProperties.put("age_limit", new FieldType("integer_range", true)); //年龄范围

        client.createIndex(INDEX_NAME, 5, 2,mappingProperties, result->{
            System.out.println(result);
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } );
    }

    /**
     * 删除索引库
     */
    public static void testDeleteIndex(){
        client.deleteIndex(INDEX_NAME, result->{
            System.out.println(result);
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

    /**
     * 是否存在
     */
    public static void testExist(){
        try {
            boolean exist = client.exist(INDEX_NAME);
            System.out.println(exist);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 打开索引
     */
    public static void testOpen(){
        client.openIndex(INDEX_NAME, result->{
            System.out.println(result);
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

    /**
     * 关闭索引
     */
    public static void testClose() {
        client.closeIndex(INDEX_NAME, result->{
            System.out.println(result);
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

    /**
     * 添加文档
     */
    public static void testInsert(){
        Map<String, Object> doc = new HashMap<>();
        doc.put("author", "张三");
        doc.put("content", "[环球时报-环球网报道 记者李司坤]在12日外交部例行记者会上，有外媒记者提问称，《环球时报》昨天报道，中国即将会对加拿大公民康明凯和迈克尔案进行一审，能否对此提供更多的细节？ ");
        doc.put("likes", Arrays.asList("游泳", "健身", "跑步"));
        doc.put("salary", 100040);
        client.insertDoc(INDEX_NAME, "2", doc, result->{
            System.out.println(result);
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

    /**
     * 根据id查询
     */
    public static void testGet() throws IOException {
        Map<String, Object> source = client.getSource(INDEX_NAME, "1");
        System.out.println(source);
        client.close();
    }


}
